package com.xiaochi.oss.service;

import com.xiaochi.oss.adapter.StorageAdapter;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * @author zqx
 * @since 2024/8/22
 */
@Service
public class FileService {

    private final StorageAdapter storageAdapter;

    public FileService(StorageAdapter storageAdapter) {
        this.storageAdapter = storageAdapter;
    }

    /**
     * 列出所有桶
     */
    public List<String> getAllBuckets(){
        return storageAdapter.getAllBuckets();
    }

    /**
     * get url
     */
    public String getUrl(String bucket,String objectName){
        return storageAdapter.getUrl(bucket,objectName);
    }

    /**
     * 上传文件
     */
    public String upload(MultipartFile uploadFile, String bucket, String objectName) {
        this.storageAdapter.uploadFile(uploadFile,bucket,objectName);
        objectName = objectName + "/" + uploadFile.getOriginalFilename();
        return storageAdapter.getUrl(bucket,objectName);
    }
}
